<template>
	<view class="content">
		<scroll-view scroll-y class="left-aside">
			<view v-for="item in flist" :key="item.classId" class="f-item b-b"
				:class="{active: item.classId === currentId}" @click="tabtap(item)">
				{{item.className}}
			</view>
		</scroll-view>
		<scroll-view scroll-with-animation scroll-y class="right-aside" @scroll="asideScroll"
			:scroll-top="tabScrollTop">
			<view v-for="item in slist" :key="item.classId" class="s-list" :id="'main-'+ item.classId">
				<text class="s-item">{{item.className}}</text>
				<view class="t-list">
					<view @click="navToList(item.classId, titem.classId)" v-if="titem.parentId === item.classId"
						class="t-item" v-for="titem in tlist" :key="titem.classId">
						<image :src="titem.iconUrl"></image>
						<text>{{titem.className}}</text>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				sizeCalcState: false,
				tabScrollTop: 0,
				currentId: 1,
				flist: [],
				slist: [],
				tlist: [],
			}
		},
		onLoad() {
			// this.loadData();
			this.getCateList();
		},
		methods: {
			async loadData() {
				let list = await this.$api.json('cateList');
				list.forEach(item => {
					if (!item.pid) {
						this.flist.push(item); //pid为父级id, 没有pid或者pid=0是一级分类
					} else if (!item.picture) {
						this.slist.push(item); //没有图的是2级分类
					} else {
						this.tlist.push(item); //3级分类
					}
				})
			},
			async getCateList() {
				let data = {
					"head": {
						"version": "v1",
						"screenWidth": 375,
						"accesstoken": "b1aa9e93-cab1-4b22-bcf2-77e4377d9a15",
						"pageCount": 1,
						"pageSize": "10"
					},
					"body": {}
				}
				
				let res = await this.helper.requestAPI(this.api.cate);
				let goodsClass = res.body.goodsClass;
				this.flist = goodsClass;
				console.log("goodsClass: ", goodsClass);
				
				goodsClass.forEach(item => {
					let childClass = item.childClass;
					childClass.forEach(item => {
						if(item.childClass.length > 0){
							this.slist.push(item);
						}
						this.tlist = this.tlist.concat(item.childClass);
					})
				})

			},
			//一级分类点击
			tabtap(item) {
				if (!this.sizeCalcState) {
					this.calcSize();
				}

				this.currentId = item.classId;
				let index = this.slist.findIndex(sitem => sitem.parentId === item.classId);
				this.tabScrollTop = this.slist[index].top;
			},
			//右侧栏滚动
			asideScroll(e) {
				if (!this.sizeCalcState) {
					this.calcSize();
				}
				let scrollTop = e.detail.scrollTop;
				let tabs = this.slist.filter(item => item.top <= scrollTop).reverse();
				if (tabs.length > 0) {
					this.currentId = tabs[0].parentId;
				}
			},
			//计算右侧栏每个tab的高度等信息
			calcSize() {
				let h = 0;
				this.slist.forEach(item => {
					let view = uni.createSelectorQuery().select("#main-" + item.classId);
					view.fields({
						size: true
					}, data => {
						item.top = h;
						h += data.height;
						item.bottom = h;
					}).exec();
				})
				this.sizeCalcState = true;
			},
			navToList(sid, tid) {
				uni.navigateTo({
					// url: `/pages/product/list?fid=${this.currentId}&sid=${sid}&tid=${tid}`
				})
			}
		}
	}
</script>

<style lang='scss'>
	page,
	.content {
		height: 100%;
		background-color: #f8f8f8;
	}

	.content {
		display: flex;
	}

	.left-aside {
		flex-shrink: 0;
		width: 200rpx;
		height: 100%;
		background-color: #fff;
	}

	.f-item {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 100%;
		height: 100rpx;
		font-size: 28rpx;
		color: $font-color-base;
		position: relative;

		&.active {
			color: $base-color;
			background: #f8f8f8;

			&:before {
				content: '';
				position: absolute;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
				height: 36rpx;
				width: 8rpx;
				background-color: $base-color;
				border-radius: 0 4px 4px 0;
				opacity: .8;
			}
		}
	}

	.right-aside {
		flex: 1;
		overflow: hidden;
		/* padding: 0 20rpx; */
		margin: 0 20rpx;
	}

	.s-item {
		display: flex;
		align-items: center;
		height: 70rpx;
		padding-top: 8rpx;
		font-size: 28rpx;
		color: $font-color-dark;
	}

	.t-list {
		display: flex;
		flex-wrap: wrap;
		width: 100%;
		background: #fff;
		padding-top: 12rpx;
		border-radius: 10rpx;

		&:after {
			content: '';
			flex: 99;
			height: 0;
		}
	}

	.t-item {
		flex-shrink: 0;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;
		width: 170rpx;
		font-size: 26rpx;
		color: #666;
		padding-bottom: 20rpx;

		image {
			width: 100rpx;
			height: 100rpx;
		}
	}
</style>
